########
# Usage: Rscript searchResult_mean_heatmap.R 'longRNA' "TRAF3,GAB2,SNX1,ID1,RALY,DYNLRB1,MCL1"
#        Rscript searchResult_mean_heatmap.R 'circRNA' "exo_circ_000452,exo_circ_000292,exo_circ_001535,exo_circ_001573,exo_circ_004802,exo_circ_039654,exo_circ_001363,exo_circ_044529,exo_circ_003784,exo_circ_000855,exo_circ_024448"
#        Rscript searchResult_mean_heatmap.R 'cell' "Adipose Tissue,CD8_naive"
#        Rscript searchResult_mean_heatmap.R 'pathway' "pathway00001,pathway00465,pathway01805,pathway03023,pathway06573,pathway09936"               
# 传入参数： targetType = 'longRNA'
#            targets = c("TRAF3", "GAB2", "SNX1","ID1","RALY","DYNLRB1","MCL1")
#
########
library(ComplexHeatmap)

library(optparse)

option_list <- list(
  make_option("--d", default = "", type = "character", help = "data file"),
  make_option("--targetType", default = "", type = "character", help = "target type"),
  make_option("--t", default = "", type = "character", help = "targets"),
  make_option("--o", default = "", type = "character", help = "output png file")
)
opt <- parse_args(OptionParser(option_list = option_list))

Args = commandArgs()
targetType = opt$targetType
targets = opt$t
targets = as.vector(unlist(strsplit(targets, ',')))
print(targetType)
print(targets)

# path = "D:\\exo\\905\\submit 6.1"
# setwd(path)
# 
# targetType = 'longRNA'
# targets = c("GDF5","MMP24","NECAB3","ID1","RALY","DYNLRB1","C20orf173","NOP53","MCL1","GPSM3")
# 
# targetType = 'circRNA'
# targets = c("exo_circ_000452","exo_circ_000292","exo_circ_001535","exo_circ_001573","exo_circ_004802","exo_circ_039654","exo_circ_001363","exo_circ_044529","exo_circ_003784","exo_circ_000855","exo_circ_024448")
# 
# targetType = 'cell'
# targets = c("Adipose Tissue", "CD8_naive")
# 
# targetType = 'pathway'
# targets = c('P00001', 'P00465', 'P01805', 'P03023', 'P06573', 'P09936')


TargetNumber = length(targets)
times = ceiling(TargetNumber/5)
w=9
h=4 * times
outfile = opt$o
datafile =opt$d

data = read.csv(datafile, sep='\t', header = TRUE, row.names=1)
data.selected = data[targets, ]
data.selected
cohortOrder = c("Urine","CSF","Bile","Healthy","Benign","BRCA","CHD","CRC","ESCC","GBM","GC","HCC","KIRC","ML","MEL","OV","PAAD","SCLC")
data.selected = data.selected[, cohortOrder]

if (targetType == 'longRNA'){
  data.selected = as.matrix(log2(data.selected+1))
  legend_name = 'log2(TPM+1)'
}else if(targetType == 'circRNA'){
  data.selected = as.matrix(log2(data.selected+1))
  legend_name = 'log2(CPM+1)'
}else if(targetType == 'pathway'){
  data.selected = as.matrix(data.selected)
  legend_name = 'ssGSEA score'
}else{
  data.selected = as.matrix(data.selected)
  legend_name = 'Absolute abundance'
}

pdf(outfile,  width=w, height=h)
p= Heatmap(data.selected, name=legend_name, border = "#BFBFBF",
           cluster_rows = F, cluster_columns = F,
           show_column_names = T, show_row_names = T,
           row_names_side = 'left', column_names_side = 'top',             #显示行/列名的位置（"left"，"right"，"top"，"bottom"）
           row_names_rot = 360, column_names_rot = 45,                     #控制行/列名的角度
           row_names_gp=gpar(fontsize = 10, fontface='bold'), column_names_gp=gpar(fontsize = 10, fontface='bold'), 
           rect_gp = gpar(col = "#F2F2F2", lwd = 1),
           cell_fun = function(j, i, x, y, width, height, fill) {
             grid.rect(x = x, y = y, width = 100, height = 5, 
                       gp = gpar(col = "white", fill = NA))},
           heatmap_legend_param=list(direction = "horizontal",
                                     legend_width = unit(5, "cm"), title_position = "topcenter"),
           show_heatmap_legend = T,
           col = colorRampPalette(c("#6776B2","white" ,"#E35C68"))(2000)
)
draw(p, merge_legend = FALSE, heatmap_legend_side = "top")
dev.off()




